package com.guigu.pd.base.controller;



import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guigu.pd.base.common.CustomIdGenerator;
import com.guigu.pd.serviceapi.pojo.TransportTrips;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.base.entity.TransportTripsEntity;
import com.guigu.pd.base.service.TransportTripsService;
import com.guigu.common.utils.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 车次信息表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:09:01
 */
@RestController
@RequestMapping("base/transporttrips")
public class TransportTripsController {
    @Autowired
    private TransportTripsService transportTripsService;

    @Autowired
    CustomIdGenerator customIdGenerator;


    /**
     * 添加车次
     * @return 车次信息
     */
    @PostMapping("/saveTransportTrips")
    public R saveTransportTrips(@RequestParam Map<String, Object> params) {
        TransportTripsEntity
                transportTrips = JSON.parseObject(JSON.toJSONString(params),
                TransportTripsEntity.class);
        transportTrips.setId(customIdGenerator.nextId(transportTrips)+"");

        boolean save = transportTripsService.save(transportTrips);

        if(save){
            return R.ok("添加成功");
        }
        return R.ok("添加失败");
    }

    /**
     * 根据id获取车次详情
     *
     * @param id 车次id
     * @return 车次信息
     */
    @GetMapping("/fineTransportTripsById/{id}")
    public TransportTripsEntity fineTransportTripsById(@PathVariable(name = "id") String id) {
        TransportTripsEntity pdTransportTrips = transportTripsService.getById(id);
        return pdTransportTrips;
    }


    /**
     * 更新车次信息
     * @return 车次信息
     */
    @PutMapping("/updateTransportTripsbyid")
    public R updateTransportTripsbyid(@RequestParam Map<String, Object> params) {
        TransportTripsEntity
                transportTrips = JSON.parseObject(JSON.toJSONString(params),
                TransportTripsEntity.class);
        boolean b = transportTripsService.updateById(transportTrips);

        if(b){
            return R.ok("修改成功");
        }
        return R.ok("修改失败");
    }

    /**
     * 删除车次信息
     * @param id 车次信息
     * @return 返回信息
     */
    @PutMapping("/delectTransportTripsbyid/{id}")
    public R delectTransportTripsbyid(@PathVariable(name = "id") String id) {
        boolean b = transportTripsService.removeById(id);
        if(b){
            return R.ok("删除成功");
        }
        return R.ok("删除失败");
    }

    @GetMapping("/querytransporttripsbytransportlineid/{transportlineid}")
    public List<TransportTrips> querytransporttripsbytransportlineid(@PathVariable("transportlineid") String transportlineid){

        LambdaQueryWrapper<TransportTripsEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TransportTripsEntity::getTransportLineId,transportlineid);
        List<TransportTripsEntity> list = transportTripsService.list(lambdaQueryWrapper);

        List<TransportTrips> list1 = new ArrayList<>();

        BeanUtils.copyProperties(list1,list);
        return list1;
    }

}
